<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>房源管理</el-breadcrumb-item>
        <el-breadcrumb-item>楼盘列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row :gutter="25">
              <el-col :span="8">
                  <el-date-picker
                    v-model="dataValue"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    size="small"
                    :picker-options="pickerOptions"
                    @change="initTime"
                    format="yyyy-MM-dd HH:mm:ss"
                    value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
              </el-col>
              <el-col :span="5">
                  <el-input size="small" clearable placeholder="楼盘名称" v-model="queryParams.name" /> 
              </el-col>
               <el-col :span="3">
                  <el-input size="small" clearable placeholder="省份" v-model="queryParams.province" /> 
              </el-col>
              <el-col :span="3">
                  <el-input size="small" clearable placeholder="城市" v-model="queryParams.city" /> 
              </el-col>
              <el-col :span="3" style="margin-bottom:15px">
                  <el-input size="small" clearable placeholder="区域" v-model="queryParams.area" /> 
              </el-col>
            </el-row>
            <el-row :gutter="25">
              <el-col :span="2" >
                  <el-button size="small" type="success" @click="queryPage" icon="el-icon-search"  v-if="permissionStrValue.indexOf('estate:query') !== -1">查询</el-button>
              </el-col>
              <el-col :span="2" >
                  <el-button size="small" type="danger" @click="clear" icon="el-icon-delete">清空</el-button>
              </el-col>
              <el-col :span="2" >
                  <el-button size="small" type="primary" @click="add" icon="el-icon-plus"  v-if="permissionStrValue.indexOf('estate:add') !== -1">新增</el-button>
              </el-col>
            </el-row>
            <el-divider></el-divider>

             <!--放表格-->
        <el-table :data="list" border stripe >
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column label="楼盘名称" width="120">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column label="地址" width="150">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.province }}-{{scope.row.city}}-{{scope.row.area}}</span>
                </template>
            </el-table-column>
            <el-table-column label="建筑年代" width="100">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.year }}年</span>
                </template>
            </el-table-column>
            <el-table-column label="建筑类型" width="100">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.type}}</span>
                </template>
            </el-table-column>
            <el-table-column label="物业费" width="100">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.propertyCost}}元/月</span>
                </template>
            </el-table-column>
            <el-table-column label="物业公司" width="100">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.propertyCompany}}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" width="160">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.createTime}}</span>
                </template>
            </el-table-column>
            <el-table-column label="修改时间" width="160">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.modifyTime}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="190">
                 <template slot-scope="scope">
                     <el-button type="primary" size="small" @click="updateById(scope.row.id)" icon="el-icon-edit" v-if="permissionStrValue.indexOf('estate:update') !== -1">编辑</el-button>
                     <el-button type="danger" size="small" @click="removeById(scope.row.id)" icon="el-icon-delete"  v-if="permissionStrValue.indexOf('estate:delete') !== -1">删除</el-button>
                 </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.currPage"
          :page-sizes="[10,20, 30, 40, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

        </el-card>


         <el-dialog
       title="新增数据"
       :visible.sync="addFlag"
       width="40%"
       @close="clearForm()">
       <el-form :model="estaeData" label-width="100px" ref="estateDataForm" label-position="left" :rules="estaeDataRules">
         <el-form-item label="楼盘名称:" prop="name">
           <el-input v-model="estaeData.name" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="省市区:" prop="province">
           <v-distpicker :province="estaeData.province" :city="estaeData.city" :area="estaeData.area" @selected="onSelected"></v-distpicker>
         </el-form-item>
         <el-form-item label="详细地址:" prop="address">
           <el-input v-model="estaeData.address" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="建筑年代:" prop="year">
           <el-input v-model="estaeData.year" size="small" style="width:250px"></el-input> 年
         </el-form-item>
         <el-form-item label="建筑类型:" prop="type">
           <el-input v-model="estaeData.type" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="物业费:" prop="propertyCost">
           <el-input v-model="estaeData.propertyCost" size="small" style="width:250px"></el-input> 元/月
         </el-form-item>
         <el-form-item label="物业公司:" prop="propertyCompany">
           <el-input v-model="estaeData.propertyCompany" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="开发商:" prop="developers">
           <el-input v-model="estaeData.developers" size="small" ></el-input>
         </el-form-item>
         
       </el-form>
       <span slot="footer" class="dialog-footer">
          <el-button @click="addFlag = false">取 消</el-button>
          <el-button type="primary" @click="addData()">确 定</el-button>
       </span>
     </el-dialog>


     <el-dialog
       title="编辑数据"
       :visible.sync="editFlag"
       width="40%"
       @close="clearForm()">
       <el-form :model="estaeData" label-width="100px" ref="estateDataForm" label-position="left" :rules="estaeDataRules">
         <el-form-item label="楼盘名称:" prop="name">
           <el-input v-model="estaeData.name" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="省市区:" prop="province">
           <v-distpicker :province="estaeData.province" :city="estaeData.city" :area="estaeData.area" @selected="onSelected"></v-distpicker>
         </el-form-item>
         <el-form-item label="详细地址:" prop="address">
           <el-input v-model="estaeData.address" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="建筑年代:" prop="year">
           <el-input v-model="estaeData.year" size="small" ></el-input>年
         </el-form-item>
         <el-form-item label="建筑类型:" prop="type">
           <el-input v-model="estaeData.type" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="物业费:" prop="propertyCost">
           <el-input v-model="estaeData.propertyCost" size="small" ></el-input>元/月
         </el-form-item>
         <el-form-item label="物业公司:" prop="propertyCompany">
           <el-input v-model="estaeData.propertyCompany" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="开发商:" prop="developers">
           <el-input v-model="estaeData.developers" size="small" ></el-input>
         </el-form-item>
         
       </el-form>
       <span slot="footer" class="dialog-footer">
          <el-button @click="editFlag = false">取 消</el-button>
          <el-button type="primary" @click="editData()">确 定</el-button>
       </span>
     </el-dialog>

       

    </div>
</template>

<script>
import VDistpicker from 'v-distpicker';
export default {
    name: 'Estate',
    components: {
        "v-distpicker": VDistpicker
    },
    data() {
        return {
            queryParams: {
                name: '',
                province: '',
                city: '',
                area: '',
                type: '',
                createTimeStart: '',
                createTimeEnd: '',
                currPage: 1,
                pageSize: 10
            },
            editFlag: false,
            list: [],
            dataValue: '',
            total: 0,
            pickerOptions: {
            shortcuts: [{
                text: '最近一周',
                onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', [start, end]);
                }
            }, {
                text: '最近一个月',
                onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit('pick', [start, end]);
                }
            }, {
                text: '最近三个月',
                onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit('pick', [start, end]);
                }
            }]
            },
            addFlag: false,
            estaeData: {
                id: '',
                name: '',
                province: '广东省',
                city: '深圳市',
                area: '南山区',
                address: '',
                year: '',
                type: '',
                propertyCost: '',
                propertyCompany: '',
                developers: '',

            },
            permissionStrValue: '',
            select: { province: '广东省', city: '深圳市', area: '南山区' },
            estaeDataRules: {
               name: [
                    { required: true, message: '请输入楼盘名称', trigger: 'blur' }
               ],
               province: [
                   { required: true, message: '请输入省市区', trigger: 'blur' }
               ],
               address: [
                   { required: true, message: '请输入详细地址', trigger: 'blur' }
               ],
               year: [
                   { required: true, message: '请输入建筑年代', trigger: 'blur' }
               ],
               type: [
                   { required: true, message: '请输入建筑类型', trigger: 'blur' }
               ],
               propertyCost: [
                   { required: true, message: '请输入物业费', trigger: 'blur' }
               ],
               propertyCompany: [
                    { required: true, message: '请输入物业公司', trigger: 'blur' }
               ],
               developers: [
                   { required: true, message: '请输入开发商名称', trigger: 'blur' }
               ]
           }
        }
    },
    created() {
        //this.queryPage()
        //获取权限值
       this.permissionStrValue = JSON.parse(window.sessionStorage.getItem('permissionList')).toString();
    },
    methods: {
        async queryPage() {
            const{data: res} = await this.$http.post('/house/estate/list-page', JSON.stringify(this.queryParams), {
                 headers: {
                            'Content-Type': 'application/json;charset=UTF-8',
                }
            })
            if(!res.success) {
                return this.$message.error(res.message);
            }
            console.log(res)
            this.list = res.data;
            this.total = res.total;
        },
        handleSizeChange(newPageSize) {
            this.queryParams.pageSize = newPageSize;
            this.queryPage();
        },
        handleCurrentChange(newPage) {
            this.queryParams.currPage = newPage;
            this.queryPage();

        },
        initTime(val) {
            if(val !== null) {
               this.queryParams.createTimeStart = val[0];
               this.queryParams.createTimeEnd = val[1];
            }
        },
        clear() {
                this.queryParams.name = '';
                this.queryParams.province = '';
                this.queryParams.city = '';
                this.queryParams.area = '';
                this.queryParams.type = '';
                this.queryParams.createTimeStart = '';
                this.queryParams.createTimeEnd = '';
                this.queryParams.currPage = 1;
                this.queryParams.pageSize = 10;
        },
        init() {
            this.estaeData.id = '';
            this.estaeData.name = '';
            this.estaeData.province = '广东省';
            this.estaeData.city = '深圳市';
            this.estaeData.area = '南山区';
            this.estaeData.address = '';
            this.estaeData.year = '';
            this.estaeData.type = '';
            this.estaeData.propertyCost = '';
            this.estaeData.propertyCompany = '';
            this.estaeData.developers = '';

        },
        add() {
            this.addFlag = true;
        },
        addData() {
            this.$refs.estateDataForm.validate(async valid => {
                if(valid) {
                    const {data: res} = await this.$http.post('/house/estate/add', JSON.stringify(this.estaeData), {
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8',
                        }
                    })
            
                    if(res.success) {
                        this.$message.success("增加成功！");
                        this.addFlag = false;
                        this.init();
                        this.queryPage();
                    } else{
                        this.$message.error(res.message);
                    }
                }
            })
        },
        onSelected(data) {
            this.estaeData.province = data.province.value;
            this.estaeData.city = data.city.value;
            this.estaeData.area = data.area.value;
        },
        async updateById(id) {
            this.editFlag = true;
            const{data: res} = await this.$http.get('/house/estate/get-one-by-id/' + id);
            if(!res.success) {
                return this.$message.error(res.message);
            }
            this.estaeData = res.data.data;
        },
        async removeById(id) {
             const confirmResult = await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => {
                return err;
            });
            //当用户确认时，则返回值为字符串：confirm
            //取消时，返回字符串cancel
            if (confirmResult !== 'confirm'){
                return this.$message.info("已取消删除")
            }
            //发送请求
            const{data: res} = await this.$http.delete('/house/estate/delete/' + id);
            if(res.success) {
                this.$message.success("删除成功！");
                this.init();
                this.queryPage();
            } else {
                this.$message.error(res.message);
            }

        },
        clearForm() {
            this.init();
        },
        async editData() {
            const{data: res} = await this.$http.put('/house/estate/update', JSON.stringify(this.estaeData), {
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8',
                }
            });
            if(res.success) {
                this.$message.success("修改成功！")
                this.editFlag = false;
                this.queryPage();
            } else {
                this.$message.error(res.message);
            }

        }
        
        

    }
    
}
</script>


<style scoped>

</style>